<?php
header('Content-Type: application/json; charset: UTF-8');

include_once(dirname(__FILE__).'/config.php');
$store = ARC2::getStore($arc_config);

function strSlice($str, $start, $end) {
    $end = $end - $start;
    return substr($str, $start, $end);
}

$q = <<<EOT
SELECT ?subject ?subjectLabel ?relationLabel ?objectLabel ?subjectThumbnail1 ?subjectThumbnail2
?subjectTypeLabel ?firstPage WHERE {
	?subject ?relation ?object .
	?vocabulary <http://desanti.org/schemas/vocabularies#hasRelation> ?relation .
	?relation rdfs:label ?relationLabel .
	?object rdfs:label ?objectLabel .
	?subject a ?subjectType .
	?subjectType rdfs:label ?subjectTypeLabel .
	OPTIONAL {
	  ?subject  rdfs:label ?subjectLabel .
	} .
	OPTIONAL {
	  ?subject <http://desanti.org/schemas/collections#thumbnail> ?subjectThumbnail1 .
	} .
	OPTIONAL {
	  ?subject <http://desanti.org/schemas/collections#pages> ?pages .
	  ?pages <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> ?firstPage .
	  ?firstPage <http://desanti.org/schemas/collections#thumbnail> ?subjectThumbnail2 .
	} .
}
EOT;

$rs = $store->query($q);
if(!$store->getErrors()){
	$rows = $rs['result']['rows'];

	for($i = 0; $i < sizeof($rows); $i++){
		$items[$rows[$i]['subject']][$rows[$i]['relationLabel']][] =
			rawurldecode($rows[$i]['objectLabel']);
		$items[$rows[$i]['subject']]['type'] = rawurldecode($rows[$i]['subjectTypeLabel']);
		$items[$rows[$i]['subject']]['id'] = $rows[$i]['subject'];
		$items[$rows[$i]['subject']]['label'] = $rows[$i]['subject'];
		if($rows[$i]['subjectLabel']){ 
			$items[$rows[$i]['subject']]['label'] = rawurldecode($rows[$i]['subjectLabel']);
		}
		if($rows[$i]['subjectThumbnail1']){
			$items[$rows[$i]['subject']]['thumbnail'] = $rows[$i]['subjectThumbnail1'];
			$items[$rows[$i]['subject']]['href'] = 'javascript:self.opener.DESANTI.openPage("' .
				$rows[$i]['subject'] . '"); self.opener.focus( )';
		}
		if($rows[$i]['subjectThumbnail2']){
			$items[$rows[$i]['subject']]['thumbnail'] = $rows[$i]['subjectThumbnail2'];
			$items[$rows[$i]['subject']]['href'] = 'javascript:self.opener.DESANTI.openPage("' .
				$rows[$i]['firstPage'] . '"); self.opener.focus( )';
		}
		
	}
}
$q = <<<EOT
SELECT ?page ?relationLabel ?text ?start ?end ?thumbnail WHERE {
  ?page <http://desanti.org/schemas/transcriptions#hasText> ?text .
  ?page <http://desanti.org/schemas/transcriptions#hasInterval> ?interval .
  ?interval <http://desanti.org/schemas/transcriptions#hasStart> ?start .
  ?interval <http://desanti.org/schemas/transcriptions#hasEnd> ?end .
  ?interval <http://desanti.org/schemas/transcriptions#hasRelation> ?relation .
  ?relation rdfs:label ?relationLabel .
  ?page <http://desanti.org/schemas/collections#thumbnail> ?thumbnail .
}
EOT;

$rs = $store->query($q);
if(!$store->getErrors()){
	$rows = $rs['result']['rows'];

	for($i = 0; $i < sizeof($rows); $i++){
		$items[$rows[$i]['page']][$rows[$i]['relationLabel']][] =
		  strSlice(rawurldecode($rows[$i]['text']), $rows[$i]['start'] - 1, $rows[$i]['end']);

		$items[$rows[$i]['page']]['type'] = 'Page';
		$items[$rows[$i]['page']]['id'] = $rows[$i]['page'];
		$items[$rows[$i]['page']]['label'] = $rows[$i]['page'];
		$items[$rows[$i]['page']]['thumbnail'] = $rows[$i]['thumbnail'];
		$items[$rows[$i]['page']]['href'] = 'javascript:self.opener.DESANTI.openPage("' .
		  $rows[$i]['page'] . '"); self.opener.focus( )';
	}
}

$items = array('items'=>array_values($items));
print_r(json_encode($items));
?>
